package com.css.bjdt_core.executive;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.bjdt_core.cell.entity.Cell;
import com.css.bjdt_core.cell.entity.CellData;
import com.css.bjdt_core.cell.service.CellDataService;
import com.css.bjdt_core.executive.report.ReportCache;
import com.css.bjdt_core.report.entity.RptTemplateUnitRela;
import com.css.bjdt_core.util.ExecutorPool;
import com.css.bjdt_core.util.spring.SpringContextHolder;

/**
 * 取消单例模式。(每一次报表请求(不同年度)都会按报表创建若干个取值线程，如果公用一个线程池，那么只要有一个取值线程在执行，将一直堵塞主线程，报表数据将无法返回)
 */
@Deprecated
public class Executors {

	private static ExecutorService pool = ExecutorPool.getInstance().getPool();

	private ReportCache cache = ReportCache.getInstance();

	private static Log logger = LogFactory.getLog(Executors.class);

	private CellDataService cellDataService = SpringContextHolder.getBean(CellDataService.class);

	private Map<String, List<CellData>> cellDataMap;

	/*private Executors() {
	}
	
	private static Executors executors;
	
	public static Executors getInstance() {
		//int cpuNum = Runtime.getRuntime().availableProcessors();
		if (executors == null) {
			executors = new Executors();
		}
		return executors;
	}*/

	/*public Executors() {
		int cpuNum = Runtime.getRuntime().availableProcessors();
		pool = java.util.concurrent.Executors.newFixedThreadPool(cpuNum);
	}*/

	public void execute(List<RptTemplateUnitRela> templateUnitRelas) {
		if (templateUnitRelas.size() > 0) {
			String annualId = templateUnitRelas.get(0).getAnnualId();
			String reportUnitId = templateUnitRelas.get(0).getReportUnitId();
			// 获取手动输入的指标值且按照报表id分组来构建map
			cellDataMap = cellDataService.buildInputCellDataMapGroupByTemplateId(annualId, reportUnitId);
			for (RptTemplateUnitRela templateUnitRela : templateUnitRelas) {
				pool.execute(new ExecuteThread(templateUnitRela));
			}
		}
	}

	// 没有事务有隐患
	private void executeByTemplate(RptTemplateUnitRela templateUnitRela) {
		List<CellData> cellDatas = new ArrayList<CellData>();
		List<Cell> cells = (List<Cell>) cache.getCell(templateUnitRela.getTemplateId());
		// 按指标取值优先级排序
		Collections.sort(cells, new Comparator<Cell>() {
			@Override
			public int compare(Cell cell1, Cell cell2) {
				return cell2.getCellLevel() - cell1.getCellLevel();
			}
		});
		List<CellData> list = new ArrayList<CellData>();
		// 把手动输入的指标值存储到list中
		if (cellDataMap.get(templateUnitRela.getTemplateId()) != null) {
			list.addAll(cellDataMap.get(templateUnitRela.getTemplateId()));
		}
		// 把list存储到cache中供其他依赖指标使用
		cache.putCellData(templateUnitRela.getReportUnitId() + templateUnitRela.getTemplateId(), list);
		for (Cell cell : cells) {
			String cellValue = cell.getExecutor().execute(cell, templateUnitRela);
			CellData cellData = new CellData(cell, cellValue, templateUnitRela.getReportUnitId());
			list.add(cellData);
		}
		cellDatas.addAll(list);
		// 按年度id、党组织id、报表id删除指标数据(只删除统计类型、计算类型、引用类型指标)
		/*cellDataService.deleteCellDataByDzzIdAndTemplateIdGy(templateUnitRela.getAnnualId(),
				templateUnitRela.getReportUnitId(), templateUnitRela.getTemplateId());*/
		// 清除缓存
		cache.clearCellData(templateUnitRela.getReportUnitId() + templateUnitRela.getTemplateId());
		cellDataService.batchInsertCellData(templateUnitRela.getAnnualId(), cellDatas);
	}

	/*public void shutdownAndAwaitTermination() {
		pool.shutdown();
		try {
			pool.awaitTermination(30, TimeUnit.MINUTES);
		} catch (InterruptedException e) {
			logger.error(e);
		} finally {
			pool = null;
		}
	}*/

	class ExecuteThread extends Thread {
		private RptTemplateUnitRela templateUnitRela;

		public ExecuteThread(RptTemplateUnitRela templateUnitRela) {
			this.templateUnitRela = templateUnitRela;
		}

		@Override
		public void run() {
			executeByTemplate(templateUnitRela);
		}

	}
}
